Developing the Hybrid Application
To understand which technology was best for each aspect of the application, I set forth to modify the original J2EE application into a hybrid application. I first examined the back end of the application, the logic and data model, and determined the best way for ColdFusion to interact with it.
The JavaBeans Component. The JavaBeans component,
TripBean, returns the detailed information about a single trip. Once
the app instantiates the bean, it can get the data directly in JSP pages
or servlets by calling the get() methods for each field
returned. For example, getTripId() outputs the trip ID.
You have access to JavaBeans components in ColdFusion MX. However, ColdFusion
makes this process somewhat easier by allowing developers to get the
fields directly (ColdFusion will automatically use the correct get()
method for you). So, for the same component, a CFML page could simply
call trip.tripID (assuming trip is the name of
the variable that represents the component).
Having access to JavaBeans data is slightly easier in CFML, but whether to use this Java class or not is still undecided. In any circumstance, where you might use a single component in other applications, you should use Java—keeping all of the logic in a single location and Java classes makes it easy to call from any Java application. However, this wasn’t the case in this hybrid application; using ColdFusion MX was a real gain for component development, especially when you look at the component syntax and simplicity.
I created a CFC to handle the same data the TripBean handled in the
J2EE version. When I called the CFC, outputting data made difference,
but I invoked the CFC with the cfinvoke tag instead of
the cfobject tag to instantiate the Java class. This made
it fairly easy for me to use a JavaBeans component if necessary. Trips.cfc
turned out to be 50 lines of code (including comments and readable line
breaks), compared to 125 lines in the Java class (also containing comments
and line breaks). In the Java class, 25 lines of were devoted to connecting
to the database and running the query. In the CFC, this took one simple
cfquery tag—five lines of code, three of which where
the actual SQL code. Also, in the JavaBeans component, I had to define
all of the get() methods as follows:
public String getDescription() {
return description;
}
Whereas in the ColdFusion code, I simply set variables to a ColdFusion
object variable, TripInfo, as follows:
TripInfo.description = getTrip.description;
Most importantly, creating the JavaBeans component requires that you know object-oriented programming, package importing, implementing other Java classes, and other concepts that require a highly-experienced object-oriented programmer. Any reasonably-skilled ColdFusion developer using basic CFML constructs could build the TripBeans CFC.
The EJB Components. EJB components handle the core transaction handling in this application. In this area, Java really shines. In the original J2EE application, the Reservation bean calls on both the Orders entity bean and CreditCard session bean. I knew for certain that in the hybrid application I would want to create Reservation.cfc to interact with these beans; this would clearly delineate between the UI and the business logic. However, the question at hand was whether this CFC should replace the Reservation bean—calling the Orders and CreditCard beans directly—or whether the CFC should just call the Reservation bean.
The answer again lay in what other applications might call this same
bean. Remember that you can access EJB components remotely, thus allowing
any application in an enterprise environment to call them. The reason
for this is so that a single element handles important business logic
processes and transactions—if you have to make any changes, you
only need to do them once, not once in every application in the enterprise.
After some thought, it seemed that making a reservation was something
that could occur from within the website, but also could very likely
occur from a point-of-sale system within the enterprise. The bean would
stay. Of course the beauty of this decision is that if I change my mind,
I would only need to change the reserve() method in the
Reservation.cfc—the CFML pages would remain the same.
Creating this CFC went relatively fast. Interestingly enough, connecting
to a bean using CFScript takes less coding than it does in Java. The
only real complexity I introduced in this area is understanding the
steps to invoke beans and making sure to use the JavaCast()
function in ColdFusion to ensure that the CFC passes the data types
to Java correctly.
The Login servlet. It is undeniable that servlet development is more time-consuming and significantly more complex than developing ColdFusion pages. Just looking at the Logon.java servlet source code will illustrate this:
- It imports a number of Java packages so the app has access to different classes directly within the servlet.
- It creates a class that extends the HttpServlet class.
- It defines an
init()method and adoPost()method. - It uses a lot of complex, object-oriented Java code throughout.
This app uses the login servlet only to perform the single action of logging in a user— the programmer must still secure the pages.
This was clearly an area where ColdFusion could shine. If you create
an Application.cfm file, ColdFusion automatically includes it in every
ColdFusion page; it could provide a lockdown mechanism. With under 40
lines of code (compared to the servlet’s 99 lines of code), I
added not only login processing, but secured every page in the site
and added the ability for users to log out. Using J2EE, a Java developer
would have had to either create a filter, a controller servlet, or include
code in every JSP page that checks whether a user is logged in; moreover,
the Java developer would need to create a process for logging out the
user (meanwhile, in ColdFusion, you use the simple cflogout
tag). The time involved in developing in ColdFusion: about 20 minutes.
This includes setting up the development environment. As a side note,
in the Application.cfm file, I also set up an abstraction for the data
source and turned on session management for the entire application (which
added two more lines of code; bringing the total to 42 lines of code,
and a few more for comments).
The JSP Pages. Once I completed the core processes, the next step was to replace the JSP pages with ColdFusion pages in the UI. It became very quickly obvious that ColdFusion was a much cleaner implementation than JSP. In literally every case, the changing JSP to CFML reduced code. There are a few primary reasons for this:
Scripting in JSP pages requires that you use Java code. This requires more lines because you must create multiple objects and use object methods to return other objects—it is more complex than CFML. JSP programmers must know Java, there is no way around that requirement.
- Retrieving information requires using
<jsp:getProperty>tags or calling methods. In ColdFusion, you only need to output the variable name. - ColdFusion has numerous built-in tools to make many processes easier. To be fair, JSP developers often use custom tag libraries to handle many of these functions. However, even if the original application used these for some functions, developers would still need to first import the tag libraries and then work with the resulting objects using object-oriented Java. There is no single JSP tag library that has as many processes or has the breadth of the CFML tag and function library.
- Strict typing requires that you manipulate variables.
In addition, JSP was simply more complex and more error prone. For a better understanding, let’s look at what specific changes I made for each of the four main pages. You can check out all of these pages in the sample app download.
home.cfm and home.jsp
This is the main page of the application. On this page, the trip names and summaries display. The app retrieves them through a query and loops through the results and output. Look at the JSP code for retrieving the information from the database:
String sql = "SELECT trip_id, name, teaser, price FROM trip";
Connection connection=null;
Statement stmt=null;
ResultSet trips=null;
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/Axion");
connection=ds.getConnection();
stmt=connection.createStatement();
trips=stmt.executeQuery(sql);
Now compare this code to the following ColdFusion, which also retrieves information from the database:
<cfquery datasource="#application.dsn#" name="getTrips"> SELECT trip_id, name, teaser, price FROM trip </cfquery>
In addition, to format the results, a Java programmer would use the
java.text.NumberFormat object:
java.text.NumberFormat nf = java.text.NumberFormat.getCurrencyInstance();
And then apply it to the number:
nf.format(trips.getDouble("price"))
This requires that a developer not only understand object-oriented
programming, but also remember numerous different classes and packages
in the Java library (there are literally hundreds of classes in the
J2EE library). You could create the same process with the CFML DollarFormat()
function:
Dollarformat(getTrips.price)
CFML has fewer functions than Java, but you can still use Java classes if you need or prefer to do so. CFML does not limit you only to CFML syntax, except for perhaps how much time you spend referring to Java language references.
Another significant difference in this page is the ease with which you can loop through the query results. In JSP, you do it as follows (this is slightly abbreviated from the initial source):
<% while (trips.next()) { %>
<tr>
<td class="contentBG">
<a href="tripdetail.jsp?tripId=<%= trips.getInt("trip_id")%>">
<%= trips.getString("name") %></a></td>
<td class="contentBG"><%= trips.getString("teaser")%></td>
<td class="contentBG"><%= nf.format(trips.getDouble("price"))%></td>
</tr>
<% } %>
There are two main issues here. First, information is output between
two scripting blocks (<% %>) because it is required that the while
statement is within two brackets ({}). The scripting element at the
end simply closes the brackets. This is not only confusing to read,
but mixing elements in this way is simply a bad practice, but inherently
built into JSP. The other issue is having to call get()
methods to return the query results—these methods are related
to the Java type being returned. Compare the ColdFusion replacement:
<cfoutput query="getTrips">
<tr>
<td class="contentBG">
<a href="tripdetail.cfm?tripId=#getTrips.trip_id#">#getTrips.name#</a></td>
<td class="contentBG">#getTrips.teaser#</td>
<td class="contentBG"> #Dollarformat(getTrips.price)#</td>
</tr>
</cfoutput>
Using only the cfoutput tag simplifies the loop, and you
output the query information using a simple format: QueryName.FieldName.
tripdetails.cfm and tripdetails.jsp
Clicking any of the trip names will take the user to the trip detail
page. This page displays the trip details and the user can make reservations.
This page calls Trips.cfc in ColdFusion, while it calls the TripBean
in the JSP version. There is little difference between creating the
bean in JSP and calling the CFC in ColdFusion. Both use tags to perform
this action. However, displaying the results reveals that using ColdFusion
is easier. In JSP, you must output each of the bean properties using
the <jsp:getProperty> tag:
<jsp:getProperty name="trip" property="dateStart"/>
This requires that you have already formatted the object (you can’t call a method on a tag). In ColdFusion, you use BeanName.PropertyName to format the output:
#trip.startdate#
You could execute any formatting functions on this variable. The syntax is shorter than in a JSP tag. Also interesting, even if ColdFusion had used the JavaBean object, the output methodology would still be the same—the only difference would be the object reference itself.
reservation.cfm and reservation.jsp
When the user is ready to reserve a trip, the app brings them to the
reservation page. The code difference in this page is nominal. However,
in the ColdFusion version, changing the input tags to CFINPUT
tags allowed me to quickly add client-side validation, such as requiring
certain fields and enforcing the user to enter his/her credit card number
in the correct format (you can use: 4111111111111111 for testing). Both
pages are 41 lines long.
Reservationaction.cfm and reservationaction.jsp
This is the page where the transaction handling, such as placing reservations
occurs. Here, you can see that ColdFusion gives us a greater degree
to separate programming and page design. In the JSP page, you call the
reserve() method of the reservation bean after connecting
to the bean, which requires that you create complex Java objects. In
contrast, the ColdFusion version uses the cfscript tag
to create an instance of the CFC and calls directly on the reserve()
method.
One core difference is also the way the JSP page creates new variables to contain information from different scopes. For example:
String number = request.getParameter("number");
You use this code for all of the variables passed to the reserve()
method, one of which are obtained using the get() methods
of the Trip JavaBeans component. You avoid this component and the CFC
replacement in the ColdFusion page by querying the price—the only
necessary data.
Other JSP changes were less noteworthy,:
- I replaced the
<jsp:include>with the cfinclude tag. - I removed the <@page> elements.
- I replaced <%= %> with
cfoutputblocks and pound signs around the variables, and so on.
These additional changes reminded me of the number of different types of elements included in JSP—and the unnecessary complexity they add. Once I completed the JSP pages, I started to create the web services interface and client application.
Submit feedback on our tutorials, articles, and sample applications.